package grandPrize

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/base-common/auth"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/grandprize"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/rpc"
	"github.com/zeromicro/go-zero/core/logc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddGrandPrizeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 新增
func NewAddGrandPrizeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGrandPrizeLogic {
	return &AddGrandPrizeLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddGrandPrizeLogic) AddGrandPrize(req *types.AddGrandPrizeReq) (resp *types.Empty, err error) {
	userId := auth.GetTokenUid(l.ctx)
	if userId == 0 {
		return nil, code.NotLogin
	}

	if req.MultiplierStart > req.MultiplierEnd || req.OrderAmountStart > req.OrderAmountEnd {
		return nil, code.ParamsInvalid
	}

	u, err := l.svcCtx.SettingService.GrandPrize.AddGrandPrize(l.ctx, &grandprize.AddGrandPrizeReq{
		MerchantId:       1,
		MultiplierStart:  float32(req.MultiplierStart),
		MultiplierEnd:    float32(req.MultiplierEnd),
		OrderAmountStart: float32(req.OrderAmountStart),
		OrderAmountEnd:   float32(req.OrderAmountEnd),
		Bonus:            float32(req.Bonus),
		Sort:             req.Sort,
		SendDay:          req.SendDay,
		Status:           rpc.StatusType(req.Status),
		Operator:         "",
		OperatorId:       userId,
	})
	if err != nil {
		logc.Error(l.ctx, "AddGrandPrize err:", err)
		return nil, err
	}
	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}
	resp = &types.Empty{}
	return
}
